@using Blazored.LocalStorage
@using Microsoft.Extensions.Options
@using Blazor.Server.UI.Models.Localization
@using System.Globalization

<MudTooltip Text="@CurrentLanguage">
    <MudMenu Class="mx-1" Icon="@Icons.Material.Outlined.Translate" Color="Color.Default" Direction="Direction.Bottom" OffsetY="true"
        Dense="true">
        @foreach (var language in SupportedLanguages)
        {
            if (language.Name == CurrentLanguage)
            {
                <MudMenuItem style="background:var(--mud-palette-primary); color:white">@language.DisplayName</MudMenuItem>
            }
            else
            {
                <MudMenuItem OnClick="(()=> ChangeLanguageAsync(language.Name))">@language.DisplayName</MudMenuItem>
            }
        }
    </MudMenu>
</MudTooltip>

@code {
    public string? CurrentLanguage { get; set; } = "en-US";
    public List<CultureInfo> SupportedLanguages { get; set; } = new();
    [Inject] private NavigationManager _navigation { get; set; }=default!;
    [Inject] private IOptions<RequestLocalizationOptions> localizationOptions { get; set; } = default!;


    protected override Task OnInitializedAsync()
    {
        SupportedLanguages = localizationOptions.Value.SupportedCultures.ToList();
        CurrentLanguage = CultureInfo.CurrentCulture.Name;
        return Task.CompletedTask;
    }


    private  Task ChangeLanguageAsync(string languageCode)
    {
        CurrentLanguage = languageCode;
        _navigation.NavigateTo(_navigation.BaseUri + "?culture=" + languageCode, forceLoad: true);
        return Task.CompletedTask;
       
    }
}
